package org.example.springbootdemo;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

@RestController
public class SessionTest {
    @RequestMapping("/session")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("name","test");
        return "设置成功";
    }
    @RequestMapping("/getsession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        return ""+name;
    }
    @RequestMapping("/getsession2")
    public String getsession2(HttpSession session){
        String name = (String) session.getAttribute("name");
        return ""+name;
    }
    @RequestMapping("/getsession3")
    public String getsession3(@SessionAttribute String name){
        return ""+name;
    }
}
